﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.SqlClient;
using System.IO;
using System.Drawing;
using System.Drawing.Text;

namespace Freshome.p.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            Models.Portalead pl = new Models.Portalead();
          
            int mn = DateTime.Today.Month;
            if (mn > 1 && mn <= 4)
            {
                pl.RightTip = new fox.Shortext("春季合理饮食", "春季是气候由寒转暖的季节，饮食中除了要保证足够的热量外，还需要需要更多的维生素C、E等和蛋白质。");
                pl.WrongTip = new fox.Shortext("春季饮食禁忌", "春季是肝旺之时，肝旺则脾虚，忌酸涩，以免肝功能偏亢，饮食宜清淡可口，忌油腻、生冷及刺激性食物。");
            }
            else if (mn > 4 && mn <= 7)
            {
                pl.RightTip = new fox.Shortext("夏季合理饮食", "夏季气温高，饮食应以清淡为主，宜选用酸性、甘凉，祛暑利湿、清热解毒、健脾养胃、滋阴补气的食物。");
                pl.WrongTip = new fox.Shortext("夏季饮食禁忌", "夏季食欲不振，应少吃油腻食物，尤其是含高热量的肉类食物，同时身处炎热的环境中，应注意饮食卫生。");
            }
            else if (mn > 7 && mn <= 10)
            {
                pl.RightTip = new fox.Shortext("秋季合理饮食", "秋季是调养生机的时节，适宜进补，可补充水分、水溶性维生素、滋阴润肺的食品，多吃新鲜水果与蔬菜。");
                pl.WrongTip = new fox.Shortext("秋季饮食禁忌", "秋天应当避免过寒过凉，少吃辣椒、葱、姜、蒜等辛辣燥烈之物，可以温性食物为主，这样利于保护胃肠。");
            }
            else
            {
                pl.RightTip = new fox.Shortext("冬季合理饮食", "冬季是需要热量的季节，应提高碳水化合物和脂肪的摄取量，同时保证优质蛋白、矿物质和维生素的供给。");
                pl.WrongTip = new fox.Shortext("冬季饮食禁忌", "冬季忌食苦寒，苦味对脾胃有损伤，大寒之物易引起泄泻。大热的食品也不宜，大热会生内热，伤及胃阴。");
            }
            return View(pl);
        }
        public PartialViewResult Technique(byte lead)
        {
            return PartialView(fox.ShortextFox.Technique(lead));
        }
        public PartialViewResult Howords(byte lead)
        {
            return PartialView(fox.ShortextFox.Howords(lead));
        }
        public PartialViewResult Notice(byte lead)
        {
            return PartialView(fox.ShortextFox.Notice(lead));
        }
        public FileContentResult Imaget(short id)
        {
            if (TempData["ImageData" + id.ToString()] != null && TempData["ImageType" + id.ToString()] != null)
            {
                return File(TempData["ImageData" + id.ToString()] as byte[], TempData["ImageType" + id.ToString()] as string);
            }
            else
            {
                RecipeBoy boy = new RecipeBoy();
                var rp = boy.GetEntity(r => r.Mid == id);
                if (rp != null) return File(rp.ImageData, "image/" + rp.ImageType);
                else return null;
            }
        }
        [OutputCache(Duration = 1)]
        public FileContentResult CaptchaImage(int seed)
        {
            var rnd = new Random(seed);
            //generate question
            int x = rnd.Next(0, 9);
            int y = rnd.Next(0, 9);
            var captcha = string.Format("{0} + {1} = ?", x, y);

            //store answer
            Session["Captcha"] = x + y;

            using (var mem = new MemoryStream())
            {
                using (var bmp = new Bitmap(88, 22))
                {
                    using (var gfx = Graphics.FromImage(bmp as Image))
                    {
                        gfx.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
                        gfx.FillRectangle(Brushes.White, new Rectangle(0, 0, bmp.Width, bmp.Height));

                        for (byte i = 1; i < 9; i++)
                        {
                            var pen = new Pen(Color.FromArgb((rnd.Next(22, 222)), (rnd.Next(22, 222)), (rnd.Next(22, 222))));
                            int r = rnd.Next(2, 20);
                            x = rnd.Next(8, 80);
                            y = rnd.Next(2, 20);
                            gfx.DrawEllipse(pen, x - r, y - r, r, r);
                        }
                        //add question
                        gfx.DrawString(captcha, new Font("Tahoma", 14), Brushes.Brown, 5, 2);
                        //render as Jpeg
                        bmp.Save(mem, System.Drawing.Imaging.ImageFormat.Jpeg);
                        return this.File(mem.GetBuffer(), "image/Jpeg");
                    }
                }
            }
        }
        public ActionResult Pairawords(short id, string nm)
        {
            PairawBoy boy = new PairawBoy();
            var prs = boy.GetEntities(m => m.Jmx == id);
            ViewBag.Majoraw = nm;
            return View(prs);
        }
    }
}
